package com.ruyuan.engine.utils;
import com.ruyuan.engine.pojo.CatchData;
import com.ruyuan.engine.pojo.ClientLog;
import com.ruyuan.engine.pojo.RuleEventExecuteParam;
import com.ruyuan.engine.pojo.RuleEventParam;
import org.apache.commons.lang.time.DateUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RuleCommonProcessUtils {

    /**
     *  事件和规则是否匹配
     * */
    public static boolean eventMatchRuleParam(ClientLog clientLog, RuleEventExecuteParam ruleEventExecuteParam) {
        //操作类型和匹配规则是否一致
        if (clientLog.getEventId().equals(ruleEventExecuteParam.getEventId())) {
            //事件属性
            Map<String, String> properties = clientLog.getProperties();
            HashMap<String, String> rulePro = ruleEventExecuteParam.getProperties();
            Set<Map.Entry<String, String>> entries = rulePro.entrySet();
            for(Map.Entry<String,String> entry:entries ) {
                if (!entry.getValue().equals(properties.get(entry.getKey()))) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public static boolean eventMatchRuleParam(ClientLog clientLog, RuleEventExecuteParam ruleEventExecuteParam,Boolean timeLimit) {
        boolean result = eventMatchRuleParam(clientLog, ruleEventExecuteParam);
        long startTime = ruleEventExecuteParam.getEventStartTime();
        long endTime = ruleEventExecuteParam.getEventEndTime();
        return result && Long.parseLong(clientLog.getDetailTime()) >= (startTime == -1?0:startTime) && endTime <= (endTime ==-1?Long.MAX_VALUE:endTime);
    }

    public static boolean eventMatchRuleParam(ClientLog clientLog, RuleEventParam ruleEventParam) {
        if (clientLog.getEventId().equals(ruleEventParam.getEventId())) {
            HashMap<String, String> properties = ruleEventParam.getProperties();
            for(String key:properties.keySet()) {
                String value = ruleEventParam.getProperties().get(key);
                if (!value.equals(clientLog.getProperties().get(key))) return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 通过正则表达式解析结果：
     * */
    public static int sequenceMathCountByRegex(String matchResult,String pattern) {
        Pattern r = Pattern.compile(pattern);
        Matcher matcher = r.matcher(matchResult);
        int count =0;
        while (matcher.find()) count ++;
        return count;
    }

    /**
     * key =>  userId:catchId
     * */
    public static CatchData generateRedisCatchResult(String key,Map<String,String> map) {
        CatchData catchData = new CatchData();
        if (key != null && !key.isEmpty()) {
            String[] keys = key.split(":");
            catchData.setUserId(keys[0]);
            catchData.setCacheId(keys[1]);
            catchData.setRedisCacheMap(map);
            return catchData;
        }else {
            return null;
        }
    }

    /**
     * 生成key
     * */
    public static String generateKey(String userId,String catchId) {
        if (userId != null && !userId.isEmpty() && catchId !=null && !catchId.isEmpty()) {
            return userId+":"+catchId;
        }else {
            return null;
        }
    }

    public static String generateKey(String key) {
        if (key !=null && !key.isEmpty()) {
            String[] keys = key.split(":");
            return keys[0]+ ":" + keys[1];
        }else {
            return null;
        }
    }

    public static String generateKey(Long starTime,Long endTime,Long createTime) {
            return starTime+":"+endTime+":"+createTime;
    }

    /**
     * 获取缓存有效时间段
     * */
    public static long splitTime(String timestamp) {
        long logTime = Long.parseLong(timestamp);
        return DateUtils.ceiling(new Date(logTime - 6 * 60 * 60 * 1000), Calendar.HOUR).getTime();
    }
}